<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>广告维护管理</title>
<jsp:include page="../../../views/layout/inc.jsp"></jsp:include>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/editPage')}"> --%>
	<script type="text/javascript">
		$.canEdit = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/delete')}"> --%>
	<script type="text/javascript">
		$.canDelete = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/grantPage')}"> --%>
	<script type="text/javascript">
		$.canProperty = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/editPwdPage')}"> --%>
	<script type="text/javascript">
		$.canResource= true;
	</script>
<%-- </c:if> --%>
<script type="text/javascript">
	var dataGrid;
	
	var type = '<%=request.getParameter("type")%>';
	console.log(type);
	if('' == type)  type = 1;
	
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			url : '/ads/dataGrid?location=' + type,
			fit : true,
			fitColumns : false,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'id',
			sortOrder : 'asc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			singleSelect : true,
			nowrap : true,
			frozenColumns : [ [ {
				field : 'id',
				title : '编号',
				hidden :true,
				width : 150,
				checkbox : true
			}, {
				field : 'title',
				title : '广告名字',
				width : 80,
				sortable : true
			}] ],
			columns : [ [  {
				field : 'content',
				title :'广告内容',
				width : 150,
				sortable : false
			},{
				field : 'url',
				title :'广告页面',
				width : 150,
				sortable : false
			},{
				field : 'images',
				title :'广告图片地址',
				width : 150,
				sortable : false
			},{
				field : 'status',
				title :'广告状态',
				width : 150,
				sortable : false,
				formatter:function(val,rec){ 
                   return 1==val  ? '<span style="color:red">关闭</span>':'<span style="color:green">开启</span>';  
				}
			},{
				field : 'orders',
				title :'优先级',
				width : 150,
				sortable : true
			},{
				field : 'gmtCreate',
				title : '创建时间',
				width : 150,
				sortable : true,
				formatter:function(val,rec){ 
					 var unixTimestamp = new Date(val);  
                     return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');   
				}
			}, {
				field : 'gmtModify',
				title : '最后修改时间',
				width : 150,
				sortable : true,
				formatter:function(val,rec){ 
					 var unixTimestamp = new Date(val);  
                    return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');  
				}
			},{
				field : 'action',
				title : '操作',
				width : 150,
				formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="修改"/>', row.id, '/images/extjs_icons/pencil.png');
					}
				
					str += '&nbsp;';
					if ($.canDelete) {
						if('0' == row.status){
							str += $.formatString('<img onclick="openFun(\'{0}\');" src="{1}" title=" 开启广告"/>', row.id, '/images/extjs_icons/newspaper/newspaper_add.png');
						} else if('1' == row.status){
							str += $.formatString('<img onclick="closeFun(\'{0}\');" src="{1}" title=" 关闭广告"/>', row.id, '/images/extjs_icons/newspaper/newspaper_delete.png');
						}
					}
				
					str += '&nbsp;';
					str += $.formatString('<img onclick="viewImage(\'{0}\');" src="{1}" title=" 查看图片"/>', row.images, '/images/extjs_icons/zoom/zoom.png');
					
					str += '&nbsp;';
					str += $.formatString('<img onclick="window.open(\'{0}\',\'_black\');" src="{1}" title=" 浏览网页"/>', row.url,'/images/extjs_icons/page/page_white_world.png');
					return str;
				}
			} ] ],
			toolbar : '#toolbar',
			onLoadSuccess : function() {
				$('#searchForm table').show();
				parent.$.messager.progress('close');

				$(this).datagrid('tooltip');
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll').datagrid('uncheckAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});

	
	

	function openFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要开启当前广告吗？', function(b) {
			if (b) {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				$.post('/ads/open', {
					id : id
				}, function(result) {
					if (result.success) {
						parent.$.messager.alert('提示', result.msg, 'info');
						dataGrid.datagrid('reload');
					}
					parent.$.messager.progress('close');
				}, 'JSON');
				
			}
		});
	}

	function closeFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要关闭当前广告吗？', function(b) {
			if (b) {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				$.post('/ads/close', {
					id : id
				}, function(result) {
					if (result.success) {
						parent.$.messager.alert('提示', result.msg, 'info');
						dataGrid.datagrid('reload');
					}
					parent.$.messager.progress('close');
				}, 'JSON');
				
			}
		});
	}

	function editFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '修改广告',
			width : 500,
			height : 400,
			href : '/ads/eidtPage?id=' + id,
			buttons : [ {
				text : '确认修改',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function addFun() {
		parent.$.modalDialog({
			title : '新增广告',
			width : 500,
			height : 400,
			href : '/ads/addPage?location=' + type,
			buttons : [ {
				text : '确认新增',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function viewImage(url){
		var div = $('<div class="view-img-content" style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:10000;text-align:center;"></div>');
		//var img = $('<img style="tet">');
		//img.attr('src',url);
		var modul = 	$( '<div class="view-img-content" style="background:#cfcfcf;opacity:0.5;position:fixed;left:0;top:0;z-index:9900"></div>').css({
			width:$('body').width(),
			height:$('body').height()
		});
		
		
		var close = '<div onclick="$(\'.view-img-content\').remove()" style="background:#999;position:absolute;right:-50px;top:-50px;width:100px;height:100px;line-height:140px;text-align:left;padding-left:20px;color:red;    border-radius: 50px;font-size:50px;    cursor: pointer;">×</div>';
		div.append(close);
		var img = new window.Image();
		img.src  = url;
		div.append(img);
		
		img.onerror = function(evt)	{
			parent.$.messager.alert('提示', '图片预览出错', 'info');
		};
		
		img.onload = function(event){
			if(typeof(img.readyState)=='undefined')
		    {
				img.readyState = 'undefined';
		    }
		  //在IE8以及以下版本中需要判断readyState而不是complete
		  if ((img.readyState=='complete'||img.readyState=="loaded")||img.readyState)	
		  { 
			  	var bodyH = $('body').height() ;
			  	var bodyW = $('body').width() ;
			  	if(img.width >bodyW){
			  		img.style.width =bodyW + 'px' ;
			  	} else if(img.height > bodyH) {
			  		img.style.height = bodyH + 'px';
			  	}
			  	
			  	div.css('line-height',bodyH + 'px');
				$('body').append(div);
			  	if(img.width >bodyW){
			  		img.style.width =bodyW  + 'px';
			  	} else if(img.height > bodyH) {
			  		img.style.height = bodyH + 'px';
			  	}
			  	$('body').append(modul);
		  }else{
					parent.$.messager.alert('提示', '图片预览出错', 'info');
		  }
		};
	}
	
	function searchFun() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function cleanFun() {
		$('#searchForm input').val('');
		dataGrid.datagrid('load', {});
	}
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',title:'查询条件',border:false" style="height: 60px; overflow: hidden;">
			<form id="searchForm">
				<table class="table table-hover table-condensed" style="display: none;">
					<tr>
						<th>广告标题</th>
						<td><input name="title" placeholder="广告标题" class="span2" /></td>
					</tr>
				
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false">
			<table id="dataGrid"></table>
		</div>
	</div>
	<div id="toolbar" style="display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/addPage')}"> --%>
			<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
<%-- 		</c:if> --%>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();">提交查询</a>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="cleanFun();">重置查询</a>
	</div>

	<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/addPage')}"> --%>
			<div onclick="addFun();" class="easyui-linkbutton" data-options="iconCls:'icon-add'">增加</div>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/delete')}"> --%>
			<div onclick="deleteFun();" data-options="iconCls:'pencil_delete'">删除</div>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/editPage')}"> --%>
<%-- 		</c:if> --%>
	</div>
</body>
</html>